//
//  SY_Par_PaymentListViewController.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 2016/12/29.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_Par_PaymentListViewController.h"
#import "SY_Par_PaymentCell.h"
#import "SY_PaymentDetailViewController.h"

@interface SY_Par_PaymentListViewController ()<UITableViewDelegate,UITableViewDataSource,reloadDataDelegate>
{
    NSInteger pageNum;
    EmptyStateView *emptyView;//空状态
    EmptyStateView *noWifiView;//无网络状态
    
    NSInteger selectNum;
    
}
@property (nonatomic, strong) UITableView *paymentTable;
@property (nonatomic, strong) NSMutableArray *paymentArr;

@end

@implementation SY_Par_PaymentListViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navBar.nav_titleLabel.text = @"校园收费";
    pageNum = 1;
    
    [self.view addSubview:self.paymentTable];
    
    [self loadDataRequest];
    
    emptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake((SCREENWIDTH-120)/2, (SCREENHEIGH-110)/2-70, 120, 110) andBackImage:@"list_empty" andBottomStr:@"暂无交费记录"];
    [self.view addSubview:emptyView];
    
    noWifiView = [[EmptyStateView alloc] initNoWifiWithFrame:CGRectMake(0, (SCREENHEIGH-64-175)/2, SCREENWIDTH, 175) andBackImage:@"noWifi" andBottomStr:@"网络出错啦,点击重新加载"];
    noWifiView.userInteractionEnabled= YES;
    noWifiView.delegate = self;
    [self.view addSubview:noWifiView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableView:) name:@"SY_PAYMENTLIST_REFRESH" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Method
//支付完成刷新单行
- (void)refreshTableView:(NSNotification *)NSNotification {
    
    [self.paymentArr replaceObjectAtIndex:selectNum withObject:NSNotification.object];
    
    [self.paymentTable reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:selectNum inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    
}

#pragma mark - Request

- (void)loadDataRequest {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_receiptlist_toparent" forKey:@"method"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:@"1" forKey:@"pageno"];
    [param setObject:@"20" forKey:@"pagesize"];
    
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        
        NSArray *dataArr = [jsonObject objectForKey:@"re_data"];
        
        self.paymentArr = [[NSMutableArray alloc]init];
        
        for (NSDictionary *dic in dataArr) {
            
            SY_Par_PaymentModel *model = [SY_Par_PaymentModel mj_objectWithKeyValues:dic];
            
            [self.paymentArr addObject:model];
            
        }
        
        [self.paymentTable.mj_header endRefreshing];
        [self.paymentTable reloadData];
        
        if (self.paymentArr.count == 0) {
            [self->noWifiView hideEmptyImage];
            [self->emptyView showEmptyImage];
        }
        
        
    }failure:^(NSInteger recode, NSError *error) {
        if (error.code == -1009) {
            
            self.paymentTable.hidden = YES;
            [self.paymentTable.mj_header endRefreshing];
            [self->emptyView hideEmptyImage];
            [self->noWifiView showEmptyImage];
            
        }
    }];
    
}

- (void)loadMoreDataRequest {
    
    pageNum += 1;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_receiptlist_toparent" forKey:@"method"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:[NSString stringWithFormat:@"%ld",pageNum] forKey:@"pageno"];
    [param setObject:@"20" forKey:@"pagesize"];
    
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        
        NSArray *dataArr = [jsonObject objectForKey:@"re_data"];
        
        if (dataArr && dataArr.count > 0) {
            for (NSDictionary *dic in dataArr) {
                
                SY_Par_PaymentModel *model = [SY_Par_PaymentModel mj_objectWithKeyValues:dic];
                
                [self.paymentArr addObject:model];
                
            }
            [self.paymentTable.mj_footer endRefreshing];
            [self.paymentTable reloadData];
            
        }else {
            self->pageNum -= 1;
            [self.paymentTable.mj_footer endRefreshingWithNoMoreData];
        }
        
        
        
        
    }failure:^(NSInteger recode, NSError *error) {
        self->pageNum -= 1;
    }];
}

#pragma mark - reloadDataDelegate

- (void)reloadCurrentData {
    
    [noWifiView hideEmptyImage];
    self.paymentTable.hidden = NO;
    
    [self loadDataRequest];
}

#pragma mark - TableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.paymentArr count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SY_Par_PaymentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    
    if (cell == nil) {
        cell = [[SY_Par_PaymentCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellIdentifier"];
    }
    
    [cell creatCell:[self.paymentArr objectAtIndex:indexPath.row]];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    selectNum = indexPath.row;
    
    SY_PaymentDetailViewController *detailVC = [[SY_PaymentDetailViewController alloc]init];
    detailVC.detailModel = [self.paymentArr objectAtIndex:indexPath.row];
    detailVC.navBar.nav_titleLabel.text = @"交费详情";
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}


#pragma mark - Get

- (UITableView *)paymentTable {
    
    if (_paymentTable == nil) {
        
        _paymentTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight)];
        _paymentTable.delegate = self;
        _paymentTable.dataSource = self;
        _paymentTable.rowHeight = 80;
        _paymentTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _paymentTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self loadDataRequest];
        }];
        
        _paymentTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self loadMoreDataRequest];
            
        }];
        
    }
    
    return _paymentTable;
}

@end
